﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Administrare
{
    public partial class AccessMenuFrm : FrmRezumat
    {

        private Guid AppID = Guid.Empty;
        private Guid RoleID = Guid.Empty;
        public AccessMenuFrm(Guid appID, Guid roleID, ModuleClass mInfo)
            : base(mInfo)
        {
            AppID = appID;
            RoleID = roleID;
            InitializeComponent();
            workConnString = "AdminConnectionString";
            tipAccessSursaBindingSource.List.Add(new TipAccessSursa("Fara Acces", ""));
            tipAccessSursaBindingSource.List.Add(new TipAccessSursa("In citire", "R"));
            tipAccessSursaBindingSource.List.Add(new TipAccessSursa("Citire si Scriere", "RW"));
        }

        private void AccessMenuFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(adminRoleInMenuBindingSource);
            listTableAdaptor.Add(admin_RoleInMenuTableAdapter.Adapter);

            SqlConnection con = DeschideConexiune(workConnString);
            this.admin_RoleInMenuTableAdapter.Connection = con;
            this.admin_RoleInMenuTableAdapter.FillByApplicationID(this.adminDataSet.admin_RoleInMenu,AppID);
            aspnetApplicationsBindingSource.Filter = "ApplicationID='" + AppID.ToString() + "'";

            this.admin_ApplicationMenuTableAdapter.Connection = con;
            this.admin_ApplicationMenuTableAdapter.FillByApplicationID(this.adminDataSet.admin_ApplicationMenu, AppID);

            this.aspnet_ApplicationsTableAdapter.Connection = con;
            this.aspnet_ApplicationsTableAdapter.Fill(this.adminDataSet.aspnet_Applications);

            if (adminRoleInMenuBindingSource.Count == 0)
            {
                SqlCommand cmd = new SqlCommand("", con);
                cmd.CommandText = "INSERT INTO [admin_RoleInMenu] (RoleId,IDMeniuAplicatie,TipAccess) SELECT '" + RoleID.ToString() + "',IDMeniuAplicatie,'RW' FROM " +
                   " [admin_ApplicationMenu] WHERE [ApplicationID]='" + AppID.ToString() + "'";
                cmd.ExecuteNonQuery();
                this.admin_RoleInMenuTableAdapter.FillByApplicationID(this.adminDataSet.admin_RoleInMenu, AppID);
            }
            InchideConexiune(con);
        }

        private void AccessMenu(object sender, EventArgs e)
        {
            adminMeniuriActiveBindingSource.EndEdit();
            SqlCommand cmd = new SqlCommand("", DeschideConexiune(workConnString));
            foreach (DataGridViewRow dr in AccessMenuDGV.Rows)
            {
                if (dr.Cells["AccessColumn"].Value != DBNull.Value)
                {
                    cmd.CommandText = "UPDATE [admin_RoleInMenu] SET [TipAccess]='" + dr.Cells["AccessColumn"].Value.ToString() + 
                        "' WHERE [RoleID] = '" + RoleID.ToString() + "' AND [IdMeniuAplicatie]=" + 
                        dr.Cells["IDMeniuColumn"].Value.ToString();
                }
                else
                {
                    cmd.CommandText = "INSERT INTO [admin_RoleInMenu] (RoleId,IDMeniuAplicatie,TipAccess) VALUES ('" + RoleID.ToString() + "'," + dr.Cells["IDMeniuColumn"].Value.ToString() + ",'" +
                        dr.Cells["AccessColumn"].Value.ToString() + "')";
                }
                cmd.ExecuteNonQuery();
            }
            InchideConexiune(cmd.Connection);
        }
    }
}
